package com.lanchetech.merchant.controller;


import com.lanchetech.bean.request.MerchantRegisterReq;
import com.lanchetech.bean.request.RegisterReq;
import com.lanchetech.bean.request.SmsCodeReq;
import com.lanchetech.bean.request.UpdatePasswordByPhoneReq;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.dao.MerchantMapper;
import com.lanchetech.entity.Merchant;
import com.lanchetech.merchant.config.JwtAuthenticationRequest;
import com.lanchetech.merchant.service.MerchantAuthService;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.dao.AdminMapper;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.lanchetech.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.util.Date;

@Api(value = "登录注册管理接口", tags = "登录注册管理接口")
@RequestMapping("/api/auth")
@RestController
public class AuthController {


    @Value("${jwt.header}")
    private String tokenHeader;

    @Autowired
    private MerchantAuthService merchantAuthService;

    @Autowired
    DefaultKaptcha defaultKaptcha;

    @Autowired
    MerchantMapper merchantMapper;
    
    @Autowired
    SmsService smsService;


    @ApiOperation("商家登录")
    @PostMapping("/login")
    public ResultData<String> createAuthenticationToken(
            @RequestBody JwtAuthenticationRequest authenticationRequest, HttpSession session) throws AuthenticationException {
        return merchantAuthService.login(authenticationRequest, session);
    }

    @ApiOperation("商家登录")
    @PostMapping("/getToken")
    public ResultData<String> getAuthenticationToken(
            @RequestBody JwtAuthenticationRequest authenticationRequest, HttpSession session) throws AuthenticationException {
        return merchantAuthService.getToken(authenticationRequest, session);
    }


    @ApiOperation("商家注册")
    @PostMapping("/register")
    public BaseResp register(@RequestBody MerchantRegisterReq req) {
        return merchantAuthService.register(req);
    }

    @ApiOperation("获取验证码图片")
    @GetMapping("/captcha")
    public ModelAndView getKaptchaImage(HttpServletResponse response, HttpSession session, @RequestParam( value = "key", defaultValue = "captchaKey") String key) throws Exception {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control",
                "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");

        String capText = defaultKaptcha.createText();
        //capText是由图形验证码随机生成的4个字符
        System.out.println("capText: " + capText);

        Long time = System.currentTimeMillis();
        try {
            session.setAttribute(key, time.toString() + "," + capText);
        } catch (Exception e) {
            e.printStackTrace();
        }

        BufferedImage bi = defaultKaptcha.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }

    @GetMapping("/test")
    public BaseResp test(){
        return new BaseResp();
    }

    @ApiOperation(value = "发送短信验证码")
    @PostMapping("/sendSmsCode")
    public BaseResp sendSms(HttpServletRequest request, HttpSession session, @RequestBody SmsCodeReq req) {
        return smsService.sendSms(request, session, req);
    }

    @ApiOperation(value = "忘记密码，手机验证并修改密码")
    @PostMapping("/updatePasswordByPhone")
    public BaseResp updatePasswordByPhone(@RequestBody UpdatePasswordByPhoneReq req) {
        return merchantAuthService.updatePasswordByPhone(req);
    }
}
